package com.efast.cafe.portal.service.doc.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.efast.cafe.framework.bean.QueryPageBase;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.framework.util.DocConstant;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.cafe.portal.bean.doc.DocFileFormBean;
import com.efast.cafe.portal.bean.doc.DocFileHisQueryBean;
import com.efast.cafe.portal.bean.doc.DocFileListParamBean;
import com.efast.cafe.portal.bean.doc.DocFilesBean;
import com.efast.cafe.portal.bean.doc.FileUploadReturnBean;
import com.efast.cafe.portal.dao.doc.DocFileHisDao;
import com.efast.cafe.portal.dao.doc.DocFilesDao;
import com.efast.cafe.portal.dao.doc.DocFolderDao;
import com.efast.cafe.portal.entity.doc.DocFileForm;
import com.efast.cafe.portal.entity.doc.DocFileHis;
import com.efast.cafe.portal.entity.doc.DocFiles;
import com.efast.cafe.portal.entity.doc.DocFolder;
import com.efast.cafe.portal.service.doc.IDocFilesService;
import com.efast.cafe.portal.service.doc.IDocFolderService;
import com.efast.cafe.portal.service.doc.IDocUserService;
import com.efast.cafe.util.StringUtil;

@Service
@Transactional
public class DocFilesServiceImpl implements IDocFilesService{
	
	@Autowired
	private DocFilesDao docFilesDao;
	@Autowired
	private DocFileHisDao docFileHisDao;
	@Autowired
	private IDocFolderService docFolderService;
	@Autowired
	private IDocUserService docUserService;
	
	@Autowired
	private DocFolderDao docFolderDao;

	@Override
	public QueryPageBase queryCompanyFilesByFolder(DocFileListParamBean queryBean) {
		if(queryBean.getFolderId()==null){
			queryBean.setFolderId(Integer.parseInt(queryBean.getId().split("_")[1]));
		}
		
		
		DocFolder folderDetail = docFolderDao.getById(SpringWebUtils.getRequestCompany(), queryBean.getFolderId());
		if(docUserService.queryDocUser().getDefaultUser()!=1){
		if(docFolderService.checkFolderPur(folderDetail.getGroupId(), DocConstant.DocPurviews.PURCODE_FILE_LIST)==0){
			throw new ServiceException("PURCODE_FILE_LIST_ERROR");//没有查询文件列表的权限
		}}
		queryBean.setCompanyCode(SpringWebUtils.getRequestCompany());
		queryBean.setUsername(SpringWebUtils.getRequestUser());
		queryBean.setPurCode(DocConstant.DocPurviews.PURCODE_FILE_LIST);
		queryBean.setResultList(docFilesDao.queryCompanyFilesByFolder(queryBean));
		queryBean.setTotalRows(docFilesDao.queryCompanyFilesCountByFolder(queryBean));
		return queryBean;
	}

	@Override
	public QueryPageBase queryMyFilesByFolder(DocFileListParamBean queryBean) {
		if(queryBean.getFolderId()==null){
			queryBean.setFolderId(Integer.parseInt(queryBean.getId().split("_")[1]));
		}
		queryBean.setCompanyCode(SpringWebUtils.getRequestCompany());
		queryBean.setUsername(SpringWebUtils.getRequestUser());
		queryBean.setResultList(docFilesDao.queryMyFilesByFolder(queryBean));
		queryBean.setTotalRows(docFilesDao.queryMyFilesCountByFolder(queryBean));
		return queryBean;
	}
	@Override
	public DocFilesBean queryFilesDetail(String type,Integer id) {
		//type=user查看我的文件夹详情不需要权限
		if(docUserService.queryDocUser().getDefaultUser()!=1){
		if(StringUtil.isNullOrBlank(type)){
		if(docFolderService.checkFolderPur(queryGroupIdByFileId(id), DocConstant.DocPurviews.PURCODE_FILE_DETAIL)==0){
			throw new ServiceException("PURCODE_PURCODE_FILE_DETAIL_ERROR");//没有查询文件列表的权限
		}
		}}
		return docFilesDao.queryFilesDetail(id);
	}

	@Override
	public void updateFilesName(String type,Integer id, String realName) {
		if(docUserService.queryDocUser().getDefaultUser()!=1){
		if(StringUtil.isNullOrBlank(type)){
		if(docFolderService.checkFolderPur(queryGroupIdByFileId(id), DocConstant.DocPurviews.PURCODE_FILE_UPDATE)==0){
			throw new ServiceException("PURCODE_PURCODE_FILE_UPDATE_ERROR");//没有修改的权限
		}}}
		if(docFilesDao.queryFilesDetail(id).getIsCheckOut()==1){
			throw new ServiceException("文件签出状态不能修改");
		}
		docFilesDao.updateFilesName(id,realName);
		
	}
	@Override
	public List<FileUploadReturnBean> saveDocFiles(String abstractStr,String keyword,Integer foldId,String formId,List<DocFiles> docFiles){
		List<FileUploadReturnBean> res = new ArrayList<>();
		List<DocFileForm> fileFormList = new ArrayList<DocFileForm>(); 
		String companyCode = SpringWebUtils.getRequestCompany();
		String username = SpringWebUtils.getRequestUser();
		for(DocFiles docFile : docFiles){
			FileUploadReturnBean bean = new FileUploadReturnBean();
			bean.setFileName(docFile.getRealName());
			docFile.setAbstractStr(abstractStr);
			docFile.setKeyword(keyword);
			docFile.setCompanyCode(companyCode);
			docFile.setCreateUser(username);
			docFilesDao.saveDocFiles(docFile);
			bean.setFileId(docFile.getId());
			DocFileForm fileForm = new DocFileForm(); 
			fileForm.setFormId(formId);
			fileForm.setFileId(docFile.getId());
			res.add(bean);
			fileFormList.add(fileForm);
		}
		//保存file与表单的关系
		docFilesDao.saveDocFileForm(fileFormList,SpringWebUtils.getRequestCompany(),SpringWebUtils.getRequestUser());
		//保存文件和文件夹的关系
		docFilesDao.saveDocFileFolder(foldId,fileFormList,SpringWebUtils.getRequestCompany(),SpringWebUtils.getRequestUser());
		return res;
	}
	
	@Override
	public void saveDocFileForm(Integer id,DocFileFormBean docFileFormBean) {
		//docFilesDao.deleteAttachmentForm(formId);
		List<DocFileForm> list=docFileFormBean.getFileForms();
		if(null!=list && list.size()>0){
			//保存file与表单的关系
			docFilesDao.saveDocFileForm(list,SpringWebUtils.getRequestCompany(),SpringWebUtils.getRequestUser());
			
			//保存文件和文件夹的关系
			docFilesDao.saveDocFileFolder(id,list,SpringWebUtils.getRequestCompany(),SpringWebUtils.getRequestUser());
		}
		
		
	}
	@Override
	public void deleteDocFiles(String type,Integer id) {
		if(docUserService.queryDocUser().getDefaultUser()!=1){
		if(StringUtil.isNullOrBlank(type)){
		if(docFolderService.checkFolderPur(queryGroupIdByFileId(id), DocConstant.DocPurviews.PURCODE_FILE_DELETE)==0){
			throw new ServiceException("PURCODE_PURCODE_FILE_DELETE_ERROR");//没有删除文件列表的权限
		}}}
		docFilesDao.deleteDocFiles(id);
	}

	@Override
	public DocFileHisQueryBean queryFilesHisVersion(DocFileHisQueryBean bean) {
		bean.setResultList(docFilesDao.queryFilesHisVersionList(bean));
		bean.setTotalRows(docFilesDao.queryFilesHisVersionCount(bean));
		
		return bean;
	}

	@Override
	public void filesCheckOut(Integer id) {
		docFilesDao.filesCheckOut(id,SpringWebUtils.getRequestUser());
		
	}

	@Override
	public void filesCancleCheckOut(Integer id) {
		
		docFilesDao.filesCancleCheckOut(id);
		
	}

	@Override
	public void updateFilesKeywordOrDigest(DocFilesBean docFilesBean) {
		docFilesBean.setLastUpdateUser(SpringWebUtils.getRequestUser());
		docFilesDao.updateFilesKeywordOrDigest(docFilesBean);
		
	}

	@Override
	public Integer queryGroupIdByFileId(Integer id) {
		return docFilesDao.queryGroupIdByFileId(id);
	}

	@Override
	public void filesCheckIn(Integer id,DocFiles newFile) {
		//文件产生一条历史记录
		docFileHisDao.insertFileHistory(id);
		//更新文件表记录
		newFile.setId(id);
		docFilesDao.filesCheckIn(newFile,SpringWebUtils.getRequestUser(),SpringWebUtils.getRequestCompany());
		
	}

	@Override
	public DocFileHis queryFilesHisById(Integer id) {
		return docFileHisDao.queryFilesHisById(id);
		 
	}

	

}
